package com.aode.playlist.view.activity;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aode.common.bean.request.SearchData;
import com.aode.common.bean.response.Mllist;
import com.aode.common.bean.response.Music;
import com.aode.playlist.R;
import com.aode.playlist.adapter.MusicAdapter;
import com.aode.playlist.databinding.ActivityHomePlaylistBinding;
import com.aode.playlist.viewmodel.HomePlayListViewModel;

import java.util.ArrayList;
import java.util.List;

public class HomePlayListActivity extends AppCompatActivity implements MusicAdapter.MusicInterface {
    private ActivityHomePlaylistBinding playlistBinding;
    private HomePlayListViewModel playListViewModel;
    private List<Music> musicList = new ArrayList<>();
    private MusicAdapter musicAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        playlistBinding = DataBindingUtil.setContentView(this, R.layout.activity_home_playlist);
        playListViewModel = ViewModelProviders.of(this).get(HomePlayListViewModel.class);
        initData();
        initMusicListener();
    }

    private void initMusicListener() {
        playListViewModel.getMusicLiveData().observe(this, listResult -> {
            if(listResult.getCode().equals("0")){
                musicList = listResult.getData();
                if(musicList!=null&&musicList.size()>0&&!musicList.isEmpty()){
                    if(musicList.get(0)==null){
                        musicList.remove(0);
                    }
                }
                initRecycler((ArrayList<Music>)musicList);
            }
        });
    }

    private void initRecycler(ArrayList<Music> musicList) {
        if(musicList!=null&&!musicList.isEmpty()&&musicList.size()>0){
            musicAdapter = new MusicAdapter(this,musicList,this);
            playlistBinding.mllistRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
            playlistBinding.mllistRecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
            playlistBinding.mllistRecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
            playlistBinding.mllistRecycler.setAdapter(musicAdapter);
        }
    }

    private void initData() {
        Mllist mllist = (Mllist) getIntent().getParcelableExtra("mllist");
        if(mllist!=null){
            playlistBinding.setMllist(mllist);
        }
        SearchData searchData = new SearchData();
        searchData.setSearch(mllist.getMlistId().toString());
        playListViewModel.getMusic(searchData);
    }


    @Override
    public void onItemClick(Music music) {

    }
}
